#mPythonType:0

# 20220409  排除0和相同数减
# 20220409  增加统计时间
# 20220409  增加开屏图案
# 20220410  增加游戏
# 20220418  增加游戏音乐
# 20220420  增加游戏背景音乐
import music
import time
import random
from mpython import *
import framebuf
import font.dvsmb_21
import font.dvsm_21

# 显示大数字============================================
def fn_display_font(_font, _str, _x, _y, _wrap, _z=0):
    _start = _x
    for _c in _str:
        _d = _font.get_ch(_c)
        if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
        if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
        oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
        framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
        _x += _d[2]

# 加法,summ=多少内============================
def fn_num_add(summ):
    oled.fill(0)
    random.seed(time.ticks_cpu())
    num_1 = random.randint(1, summ-1)   #1开始
    num_2 = random.randint(1, summ - num_1)
    fn_display_font(font.dvsmb_21, str(num_1) + ' + ' + str(num_2) + ' = ?', 10, 8, False, 2)
    
    # 答案位置
    da = num_1 + num_2
    da_p = random.randint(0,2)  #答案位置0-2
    da_1 = da - da_p
    fn_display_font(font.dvsmb_21, str(da_1), 4, 40, False, 2)
    fn_display_font(font.dvsmb_21, str(da_1+1), 52, 40, False, 2)
    fn_display_font(font.dvsmb_21, str(da_1+2), 100, 40, False, 2)
    oled.rect(1, 36, 31, 25, 1)
    oled.rect(49, 36, 31, 25, 1)
    oled.rect(97, 36, 31, 25, 1)
    oled.show()
    
    return str(num_1) + ' + ' + str(num_2) + ' = ' + str(da), da_p #返回位置

# 减法法,summ=多少内=====================
def fn_num_sub(summ):
    oled.fill(0)
    random.seed(time.ticks_cpu())
    num_1 = random.randint(2, summ) #1开始
    num_2 = random.randint(1, num_1-1)
    fn_display_font(font.dvsmb_21, str(num_1) + ' - ' + str(num_2) + ' = ?', 10, 8, False, 2)
    
    # 答案位置
    da = num_1 - num_2
    da_p = random.randint(0,2)  #答案位置0-2
    da_1 = da - da_p
    fn_display_font(font.dvsmb_21, str(da_1), 4, 40, False, 2)
    fn_display_font(font.dvsmb_21, str(da_1+1), 52, 40, False, 2)
    fn_display_font(font.dvsmb_21, str(da_1+2), 100, 40, False, 2)
    oled.rect(1, 36, 31, 25, 1)
    oled.rect(49, 36, 31, 25, 1)
    oled.rect(97, 36, 31, 25, 1)
    oled.show()
    
    return str(num_1) + ' - ' + str(num_2) + ' = ' + str(da), da_p #返回位置
    
# 答题反馈==========================
def fn_select_da(pos):
    if pos == DA_pos:
        oled.fill(0)
        oled.blit(image_picture.load('face/Expressions/Smile.pbm', 0), 22, 8)
        fn_display_font(font.dvsm_21, STR_problem, 10, 1, False, 2)
        oled.show()
        music.play(music.JUMP_UP, wait=False, loop=False)
    else:
        oled.fill(0)
        oled.blit(image_picture.load('face/Expressions/Sad.pbm', 0), 22, 8)
        fn_display_font(font.dvsm_21, STR_problem, 10, 1, False, 2)
        oled.show()
        music.play(music.JUMP_DOWN, wait=False, loop=False)
        
    return (pos == DA_pos)

# 答题统计=================================
def fn_display_ss():
    #计算答题时间
    # print((time.time()-TIM_start))
    #显示统计
    oled.fill(0)
    oled.blit(image_picture.load('face/Information/Thumbs up.pbm', 0), -8, 0)
    #oled.blit(image_picture.load('face/Information/Thumbs down.pbm', 0), 52, 24)
    fn_display_font(font.dvsmb_21, str(NUM_right), 72, 10, False, 2)
    oled.DispChar(str('题'), 112, 10, 1)
    fn_display_font(font.dvsmb_21, str(time.time()-TIM_start), 72, 40, False, 2)
    oled.DispChar(str('秒'), 112, 40, 1)
    oled.show()
    music.play(music.PYTHON, wait=False, loop=False)

########################################################################
COLOR_now = (0, 0, 0)
IS_answer = False   #是否回答
IS_right = False    #是否正确答案
IS_sleep = True    #是否休眠
IS_Add = True       #是否加法
IS_Game = False     #游戏时刻
IS_start = True     #是否开屏
MOD_summ = 2    #默认混合，0加分、1减法、2混合加减法
NUM_new = 5      #换题倒计时
NUM_right = 0   #正确数量
NUM_error = 0   #错误数量
NUM_err_light = 0   #错误灯
NUM_summ = 20   #多少内加减法
NUM_ss = 20     #每次多少题
TIM_start = time.time()
# 游戏参数
Gam_score = 0
Gam_x = 63
Gam_y = 31
Gam_cx = 1
Gam_cy = -1
Gam_Lx = 50
# print(TIM_start)

image_picture = Image()
music.set_tempo(ticks=4, bpm=120)
rgb.fill(COLOR_now)
rgb.write()
time.sleep_ms(1)

#开屏图案
oled.fill(0)
oled.blit(image_picture.load('face/Objects/Flowers.pbm', 0), 16, 0)
oled.show()
    
# 问题和答案位置
# STR_problem, DA_pos = fn_num_add(NUM_summ)

while True:
    if not IS_sleep:    #做题时刻
        if not IS_answer :  #没回答
            if touchPad_P.read() < 400:
                IS_right = fn_select_da(0)
                IS_answer = True
            elif touchPad_Y.read() < 400:
                IS_right = fn_select_da(0)
                IS_answer = True
            elif touchPad_T.read() < 400:
                IS_right = fn_select_da(1)
                IS_answer = True
            elif touchPad_H.read() < 400:
                IS_right = fn_select_da(1)
                IS_answer = True
            elif touchPad_O.read() < 400:
                IS_right = fn_select_da(2)
                IS_answer = True
            elif touchPad_N.read() < 400:
                IS_right = fn_select_da(2)
                IS_answer = True
            
            if IS_answer:    
                NUM_new = 5
                if IS_right:
                    NUM_right = NUM_right +1
                    NUM_err_light = NUM_err_light -1
                    if NUM_err_light < 0: NUM_err_light = 0
                    if NUM_err_light < 3:
                        rgb[NUM_err_light] = (0, 0, 0)
                        rgb.write()
                        time.sleep_ms(1)                
                else:
                    NUM_error = NUM_error +1
                    NUM_err_light = NUM_err_light +1
                    COLOR_now = (int(255), int(0), int(0))
                    if NUM_err_light > 3: NUM_err_light = 3
                    if NUM_err_light > 0:
                        rgb[NUM_err_light-1] = COLOR_now
                        rgb.write()
                        time.sleep_ms(1)
        else:   #回答
            time.sleep_ms(120)
            NUM_new = NUM_new -1
    
    if IS_sleep and IS_Game: #游戏时刻
        oled.fill(0)
        oled.fill_rect(Gam_Lx, 62, 30, 2, 1)
        oled.DispChar(str((str(int(Gam_score)))), 0, 0, 1)
        oled.circle(Gam_x, Gam_y, 3, 1)
        oled.show()
        
        if touchPad_P.read() < 400:
            Gam_Lx = Gam_Lx + -4
            if Gam_Lx <= 0:
                Gam_Lx = 0
        elif touchPad_Y.read() < 400:
            Gam_Lx = Gam_Lx + -2
            if Gam_Lx <= 0:
                Gam_Lx = 0
        elif touchPad_O.read() < 400:
            Gam_Lx = Gam_Lx + 2
            if Gam_Lx >= 107:
                Gam_Lx = 107
        elif touchPad_N.read() < 400:
            Gam_Lx = Gam_Lx + 4
            if Gam_Lx >= 107:
                Gam_Lx = 107

        #print(int(Gam_score/10 +1))
        Gam_x = Gam_x + Gam_cx*int(Gam_score/10 +1) #加速
        Gam_y = Gam_y + Gam_cy*int(Gam_score/10 +1)
        if Gam_x <= 2:
            Gam_cx = 1
        if Gam_x >= 125:
            Gam_cx = -1
        if Gam_y <= 2:
            Gam_cy = 1
        if Gam_y > 60:
            if Gam_x >= Gam_Lx - 2 and Gam_x <= Gam_Lx + 32:
                Gam_score = Gam_score + 1
                Gam_cy = -1
                #music.play('F4:2')
                music.play(music.POWER_UP, wait=False, loop=False)
            else:
                IS_Game = False
                oled.fill(0)
                oled.blit(image_picture.load('face/Objects/Bomb.pbm', 0), -8, 0)
                #oled.blit(image_picture.load('face/Information/Thumbs down.pbm', 0), 52, 24)
                fn_display_font(font.dvsmb_21, str(Gam_score), 80, 25, False, 2)
                oled.DispChar(str('分'), 112, 25, 1)
                oled.show()
                music.play(music.PUNCHLINE, wait=False, loop=False)       

    if button_a.was_pressed():   #切换加减法/游戏----#人工休眠
        if IS_sleep:
            if IS_start: #开屏，进入计算时刻
                TIM_start = time.time()    #记录开始时间
                IS_start = False
                NUM_new = 5
                IS_answer = False
                IS_right = False
                IS_sleep = False
                IS_Game = False
                # 出题
                if IS_Add:
                    STR_problem, DA_pos = fn_num_add(NUM_summ)
                else:
                    STR_problem, DA_pos = fn_num_sub(NUM_summ)
            else: #进入游戏时刻
                IS_Game = True
                # 游戏参数
                Gam_score = 0
                Gam_x = 63
                Gam_y = 31
                Gam_cx = 1
                Gam_cy = -1
                Gam_Lx = 50
                oled.fill(0)
                oled.fill_rect(Gam_Lx, 62, 30, 2, 1)
                oled.DispChar(str((str(int(Gam_score)))), 0, 0, 1)
                oled.circle(Gam_x, Gam_y, 3, 1)
                oled.show()
    
                Gam_x = Gam_x + Gam_cx
                Gam_y = Gam_y + Gam_cy
        else:   #切换模式 0》1》2》0
            NUM_new = 5
            IS_answer = False
            IS_right = False
            IS_sleep = False
            
            if MOD_summ == 2:
                MOD_summ = 0
                IS_Add = True   #加法
            else:
                MOD_summ = MOD_summ +1
                if MOD_summ == 1:
                    IS_Add = False  #减法
                else:
                    IS_Add = not IS_Add #混合
                
            # 出题
            if IS_Add:
                STR_problem, DA_pos = fn_num_add(NUM_summ)
            else:
                STR_problem, DA_pos = fn_num_sub(NUM_summ)        
    elif button_b.was_pressed(): #人工新题
        if IS_sleep: 
            TIM_start = time.time()    #记录开始时间
            IS_start = False
        NUM_new = 5
        IS_answer = False
        IS_right = False
        IS_sleep = False
        IS_Game = False
        # 出题
        if MOD_summ == 2: IS_Add = not IS_Add   #混合模式轮回加减法
        if IS_Add:
            STR_problem, DA_pos = fn_num_add(NUM_summ)
        else:
            STR_problem, DA_pos = fn_num_sub(NUM_summ)
    elif NUM_new == 0: #新题或自动
        NUM_new = 5
        IS_answer = False
        IS_right = False
        IS_sleep = False
        if NUM_right + NUM_error < NUM_ss:
            if MOD_summ == 2: IS_Add = not IS_Add   #混合模式轮回加减法
            if IS_Add:
                STR_problem, DA_pos = fn_num_add(NUM_summ)
            else:
                STR_problem, DA_pos = fn_num_sub(NUM_summ)
        else:
            IS_sleep = True
            IS_start = False
            fn_display_ss()
            NUM_right = 0   #正确数量
            NUM_error = 0   #错误数量
            NUM_err_light = 0   #错误灯            

